package br.ucb.cesbdao.persistencia.factory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import br.ucb.cesbdao.servicos.ResourceBundleUtil;
public class FactoryConnection {

	private FactoryConnection() {

	}

	private static FactoryConnection fabrica;

	public static FactoryConnection getInstance() {
		if(fabrica==null)
			fabrica=new FactoryConnection();
		return fabrica;
	}

	public Connection criaConexao(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/daocesb";
//			String usuario = ResourceBundleUtil.getPropriedade("USUARIO");
//			String senha = ResourceBundleUtil.getPropriedade("SENHA");
			Connection conexao = DriverManager.getConnection(url,"root","root");
			return conexao;
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Connection criaConexaoFirst() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/";
		String usuario = ResourceBundleUtil.getPropriedade("USUARIO");
		String senha = ResourceBundleUtil.getPropriedade("SENHA");
		Connection conexao = DriverManager.getConnection(url,usuario,senha);
		return conexao;
	}
	
	public void closeConexao(Connection con){
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void closeConexao(PreparedStatement stm){

		try {
			stm.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void closeConexao(PreparedStatement stm, ResultSet rs){
		try {
			rs.close();
			stm.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}